﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;

namespace Fotobanka
{
    /// <summary>
    /// Obsluzna trida pro praci s komentari
    /// </summary>
    public class CommentsManager
    {
        SqlConnection connection;
        SqlCommand command;
        SqlDataReader reader;

        /// <summary>
        /// Metoda pro ziskani vsech komentaru co patri k fotce
        /// </summary>
        /// <param name="photo_id">Id fotky pro kterou se hledaji komentare</param>
        /// <param name="role">Uzivatelska role</param>
        /// <param name="usr_id">Id prihlaseneho uzivatele</param>
        /// <param name="filename">hash fotky kvuli sestaveni delete odkazu</param>
        /// <returns>seznam komentaru</returns>
        public List<CommentsObject> SelectCommentsForPhoto(int photo_id, Utils.SystemRoles role, int usr_id, String filename)
        {
            List<CommentsObject> l = new List<CommentsObject>();
            using (connection = new SqlConnection(ConfigurationManager.ConnectionStrings["amalka"].ToString()))
            {
                connection.Open();
                command = new SqlCommand("SELECT Comments.id, Comments.photo_id, Comments.user_id, Comments.text, Comments.created_on, Users.first_name, Users.last_name FROM Comments INNER JOIN Users ON Comments.user_id = Users.id WHERE (Comments.photo_id = @id)", connection);
                command.Parameters.Add(new SqlParameter("@id", photo_id));

                reader = command.ExecuteReader();

                while (reader.Read())
                {
                    CommentsObject o = new CommentsObject();
                    o.Photo_id = (int)reader["photo_id"];
                    o.Text = reader["text"].ToString();
                    o.User_id = (int)reader["user_id"];
                    o.Created_on = (DateTime)reader["created_on"];
                    o.Jmeno = reader["first_name"].ToString();
                    o.Prijmeni = reader["last_name"].ToString();

                    switch ((int)role)
                    {
                        case 1:
                            o.DeleteLink = "<a href=\"UkazFoto.aspx?h="+filename+"&pid="+photo_id+"&cd="+ reader["id"].ToString()+"\" runat=\"server\">Smazat</a>";
                            break;
                        default:
                            if (o.User_id == usr_id)
                            {
                                o.DeleteLink = "<a href=\"UkazFoto.aspx?h=" + filename + "&pid=" + photo_id + "&cd=" + reader["id"].ToString() + "\" runat=\"server\">Smazat</a>";
                            }
                            else
                            {
                                o.DeleteLink = "";
                            }

                            break;
                    }

                    l.Add(o);
                }
            }

            return l;
        }

        /// <summary>
        /// Metoda pro vlozeni komentare k dane fotce
        /// </summary>
        /// <param name="c">naplneny objekt Komentar</param>
        public void InsertComment(CommentsObject c)
        {
            using (connection = new SqlConnection(ConfigurationManager.ConnectionStrings["amalka"].ToString()))
            {
                connection.Open();

                command = new SqlCommand("INSERT INTO Comments (text, photo_id, user_id, created_on, update_on) VALUES (@text, @photo_id, @user_id, @created_on, @update_on);", connection);
                command.Parameters.Add(new SqlParameter("@text", c.Text));
                command.Parameters.Add(new SqlParameter("@photo_id", c.Photo_id));
                command.Parameters.Add(new SqlParameter("@user_id", c.User_id));
                command.Parameters.Add(new SqlParameter("@created_on", DateTime.Now));
                command.Parameters.Add(new SqlParameter("@update_on", DateTime.Now));

                command.ExecuteNonQuery();
            }
        }

        /// <summary>
        /// Metoda pro update komentare
        /// </summary>
        /// <param name="c">naplneny objekt Komentar</param>
        public void UpdateComment(CommentsObject c)
        {
            using (connection = new SqlConnection(ConfigurationManager.ConnectionStrings["amalka"].ToString()))
            {
                connection.Open();

                command = new SqlCommand("UPDATE Comments SET text=@text, update_on=@update_on WHERE photo_id=@id;", connection);
                command.Parameters.Add(new SqlParameter("@text", c.Text));
                command.Parameters.Add(new SqlParameter("@updated", DateTime.Now));

                command.ExecuteNonQuery();
            }
        }

        /// <summary>
        /// Metoda pro smazani komentare podle jeho ID
        /// </summary>
        /// <param name="id">ID komentare ke smazani</param>
        public void deleteComment(int id)
        {
            using (connection = new SqlConnection(ConfigurationManager.ConnectionStrings["amalka"].ToString()))
            {
                connection.Open();

                command = new SqlCommand("DELETE FROM Comments WHERE (id = @id);", connection);
                command.Parameters.Add(new SqlParameter("@id", id));

                command.ExecuteNonQuery();
            }
        }

    }
}
